package com.noassertions.web;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;

import net.sf.json.JSONObject;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.noassertions.data.DAOInterface;

@Controller
public class HomeController {
	protected final Log log = LogFactory.getLog(getClass());
	private DAOInterface dao;

    @Autowired
    public HomeController(DAOInterface dao) {
        this.dao = dao;
    }  
	
	@RequestMapping("/home")
	public String getRider(@RequestParam(required=false) Integer riderid, Model model) throws MalformedURLException, IOException
	{
		// If a riderid isn't passed in, grab a random one
		if (riderid == null)
		{
			Random randomGenerator = new Random();
			// get number between 1 and number of riders in the DB
			riderid = randomGenerator.nextInt(dao.getRiderCount().intValue())+1;
			log.debug("No riderid supplied, so using the random id of: " + riderid);
		}

		//Add riderid to model so that we can use it for ajax requests
		model.addAttribute("riderid", riderid);
		
		//Get rider data from rider JSON service, this is used to populate the intial numbers on the home view
		//TODO: add things like the following url to a java props file (spring resource file)
		InputStream inputStream = new URL("http://localhost:8080/noassertions/access/rider/" + riderid).openStream();
		String jsonString = IOUtils.toString(inputStream);
		IOUtils.closeQuietly(inputStream);
		
		log.debug("JSON object for rider: " + jsonString);
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		model.addAttribute("rider", jsonObject);
		
		//Get fit data from fit JSON service, this is used to populate the intial numbers on the home view
		//TODO: get url from props file
		inputStream = new URL("http://localhost:8080/noassertions/access/fit/" + riderid).openStream();
		jsonString = IOUtils.toString(inputStream);
		IOUtils.closeQuietly(inputStream);
		
		log.debug("JSON object for fit: " + jsonString);
		jsonObject = JSONObject.fromObject(jsonString);
		model.addAttribute("numFits", jsonObject.getJSONArray("fits").size());
		model.addAttribute("fit", jsonObject);
		
		//Get image data from first fitid of riderid
		JSONObject firstElement = jsonObject.getJSONArray("fits").getJSONObject(0);
		int firstFitId =firstElement.getInt("fitid"); 
		
		//TODO: get url from props file
		inputStream = new URL("http://localhost:8080/noassertions/access/image/" + firstFitId).openStream();
		jsonString = IOUtils.toString(inputStream);
		IOUtils.closeQuietly(inputStream);
		
		log.debug("JSON object for image: " + jsonString);
		jsonObject = JSONObject.fromObject(jsonString);
		model.addAttribute("image", jsonObject);
		//HACK: This minus one business is so that we can use the JSTL For Each construct
		model.addAttribute("numImagesMinusOne", (jsonObject.getJSONArray("images").size()-1));
		
		return "home";
	}
}